#!/bin/bash

# 惠企政策爬虫安装脚本

echo "====== 开始安装惠企政策爬虫依赖 ======"

# 安装Node.js依赖
echo "正在安装依赖..."
npm install --save cheerio axios node-cron winston

# 创建数据库表
echo "正在创建数据库表..."
node -e "
const mysql = require('mysql2/promise');
const dotenv = require('dotenv');
const fs = require('fs');
const path = require('path');

// 加载环境变量
dotenv.config({ path: '.env.local' });

async function setupDatabase() {
  try {
    // 数据库配置
    const dbConfig = {
      host: process.env.DB_HOST || 'rm-bp14wd51i709c3z79ho.mysql.rds.aliyuncs.com',
      user: process.env.DB_USER || 'root',
      password: process.env.DB_PASSWORD || 'Ircnjfdhnvxwz@123',
      database: process.env.DB_NAME || 'xiaoyoubao',
      port: Number(process.env.DB_PORT) || 3306
    };
    
    console.log('正在连接数据库...');
    const connection = await mysql.createConnection(dbConfig);
    console.log('数据库连接成功');
    
    console.log('正在创建惠企政策表...');
    const sql = fs.readFileSync(path.join(__dirname, '../sql/create_policies_table.sql'), 'utf8');
    
    // 分割SQL语句并执行
    const statements = sql.split(';').filter(stmt => stmt.trim());
    
    for (const statement of statements) {
      if (statement.trim()) {
        await connection.execute(statement + ';');
      }
    }
    
    console.log('数据库表创建成功');
    await connection.end();
    
    console.log('数据库设置完成');
  } catch (error) {
    console.error('设置数据库失败:', error);
    process.exit(1);
  }
}

setupDatabase();
"

# 创建日志目录
echo "正在创建日志目录..."
mkdir -p logs

# 设置DeepSeek API密钥
echo "设置DeepSeek API密钥..."
grep -q "DEEPSEEK_API_KEY" .env.local || echo "DEEPSEEK_API_KEY=sk-dbc2528168b542acbf58df81f0c3db88" >> .env.local

# 设置可执行权限
chmod +x scripts/crawl_policies.js
chmod +x scripts/schedule_policy_crawler.js

echo "====== 惠企政策爬虫安装完成 ======"
echo "你可以使用以下命令运行爬虫:"
echo "  npm run crawl-policies     # 手动执行一次爬虫"
echo "  npm run schedule-policies  # 启动定时爬虫任务" 